/*
 * OpenParts
 * A dynamic-loading components framework for GWT
 * Copyright (C) 2011 Christophe Bouthier  [chris{AT}binary-gastronome{DOT}fr]
 *
 * This work is partially based on work I have done at INRIA (http://www.inria.fr) 
 * in the context of the Qualipso European Project (http://qualipso.org/),
 * The work done at INRIA is Copyright (C) 2006-2011 INRIA
 *
 * This work is distributed under the LGPL version 3
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation. See the GNU
 * Lesser General Public License in LGPL.txt for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package fr.openparts.opshowcase.errorsdummyservice.client.presenter;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.Widget;

import fr.openparts.OpenParts.client.OPBinder;
import fr.openparts.OpenParts.client.OPCommandCallback;
import fr.openparts.OpenParts.client.OPCommandFailureReason;
import fr.openparts.OpenParts.client.OPMessagesDispatcher;
import fr.openparts.OpenParts.client.OPParams;
import fr.openparts.OpenParts.client.OPShell;
import fr.openparts.OpenParts.client.OPSlot;
import fr.openparts.OpenParts.client.OPSlotProxy;
import fr.openparts.OpenParts.client.annotations.OPMandatoryParams;
import fr.openparts.OpenParts.client.annotations.OPServiceName;
import fr.openparts.OpenParts.client.annotations.OPViewFactory;
import fr.openparts.opshowcase.errorsdummyservice.client.view.ErrorsDummyServiceView;

/**
 * Dummy service to test errors messages.
 * 
 * @author Christophe Bouthier [chris{AT}binary-gastronome{DOT}fr]
 * @creation.date 13 April 2011
 */
@OPServiceName("errorsdummyservice")
public class ErrorsDummyServicePresenter implements EntryPoint {

    public interface ErrorsDummyServiceBinder extends OPBinder<ErrorsDummyServicePresenter> {}
    // need to remove the static here due to a bug in the optimization done by GWT compiler (issue #6283)
    // see http://code.google.com/p/google-web-toolkit/issues/detail?id=6283
    public final ErrorsDummyServiceBinder binder = GWT.create(ErrorsDummyServiceBinder.class);
    
    protected final static String UNKNOWN_SLOT = "unknownSlot1";
    protected final static String DUMMY_MESSAGE = "dummyMessage";
    protected final static String DUMMY_ID = "dummyId1";
    
    /**
     * Entry point, method called when the module is loaded.
     * 
     * @see com.google.gwt.core.client.EntryPoint#onModuleLoad()
     */
    @Override
    public void onModuleLoad() {
        binder.bindPart(this);
        binder.notifyLoadingCompleted();
    }

    /**
     * Returns a view
     * 
     * @param params
     *            not used here
     * @return a view
     */
    @OPViewFactory(resourceName = "errorsdummyresource", actionName = OPShell.ACTION_DISPLAY)
    @OPMandatoryParams("mandatoryParam")
    public Widget getDummyView(final OPParams params, final OPSlotProxy proxy) {
        return new Label();
    }
    
    /**
     * Returns the sub view of the dummy service
     * 
     * @param params
     *            not used here
     * @return the view to embed
     */
    @OPViewFactory(resourceName = "errorsembeddedresource", actionName = OPShell.ACTION_DISPLAY)
    public Widget getSubView(final OPParams params, final OPSlotProxy proxy) {
        return new ErrorsDummyServiceView(this);
    }

    /**
     * No such slot for a dispatch to slot message
     */
    public void generateNoSuchSlotIdInThePageSlotErrorMessage(final Label label) {
        final OPSlotProxy proxy = new OPSlotProxy(UNKNOWN_SLOT);
        proxy.dispatchMessageToContainerSlot(DUMMY_MESSAGE, null, new OPCommandCallback() {
            @Override
            public void onSuccess() {
                // no way we should get here
                label.setText("Error in the test, OPSlotProxy found an unknown slot !!! This is not what should happens !!!");
            }
            @Override
            public void onFail(OPCommandFailureReason reason) {
                label.setText(reason.getDescription());
            }
        });
    }

    /**
     * Not a slot for a dispatch to slot message
     */
    public void generateGivenIdIsNotASlotSlotErrorMessage(final Panel container, final Label label) {
        final FlowPanel falseSlot = new FlowPanel();
        falseSlot.getElement().setId(DUMMY_ID);
        container.add(falseSlot);
        final OPSlotProxy proxy = new OPSlotProxy(DUMMY_ID);
        proxy.dispatchMessageToContainerSlot(DUMMY_MESSAGE, null, new OPCommandCallback() {
            @Override
            public void onSuccess() {
                // no way we should get here
                label.setText("Error in the test, OPSlotProxy found something that's not a slot !!! This is not what should happens !!!");
                container.remove(falseSlot);
            }
            @Override
            public void onFail(OPCommandFailureReason reason) {
                label.setText(reason.getDescription());
                container.remove(falseSlot);
            }
        });
    }

    /**
     * No view attached to slot proxy
     */
    public void generateNoSlotAttachedToProxyErrorMessage(final Panel container, final Label label) {
        final OPSlot slot = OPShell.getSlot(DUMMY_ID);
        container.add(slot);
        final OPSlotProxy proxy = new OPSlotProxy(DUMMY_ID);
        final OPMessagesDispatcher dispatcher = OPMessagesDispatcher.dispatcherFactory(DUMMY_ID);
        dispatcher.attachDispatcherOnElement(slot.getElement());
        proxy.dispatchMessageToContainerSlot(DUMMY_MESSAGE, null, new OPCommandCallback() {
            @Override
            public void onSuccess() {
                // no way we should get here
                label.setText("Error in the test, OPSlotProxy found a slot attached t !!! This is not what should happens !!!");
                container.remove(slot);
            }
            @Override
            public void onFail(OPCommandFailureReason reason) {
                label.setText(reason.getDescription());
                container.remove(slot);
            }
        });
    }

}
